package com.qys.livesMall.main.test;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dp
 * Date: 2023/05/22
 * Time: 16:17
 * DESCRIPTION:
 */
public class CodeGenerator {
    public static void main(String[] args) throws Exception {
        Connection conn = DriverManager.getConnection("jdbc:mysql://223.86.169.32:3306/lives-mall?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"
                + "&user=lives-mall&password=Xyw8Pf6FdZFDibah");
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("show tables; ");
        List<String> tables = new ArrayList<>();
        tables.add("h2_sys_operate_report_forms");
//        while (rs.next()) {
//            System.out.println(rs.getString("Tables_in_cloud-system"));
//            tables.add(rs.getString("Tables_in_cloud-system"));
//
//        }
        String[] strings = new String[tables.size()];

        String projectPath = System.getProperty("user.dir") + "/qys-lives-mall-shop/";
        System.out.println(projectPath);

        String url = "jdbc:mysql://223.86.169.32:3306/lives-mall-temp?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig.Builder(url, "lives-mall-temp", "WTTp2mHWb64DhCLM");

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                //全局配置
                .globalConfig(builder ->
                        builder.author("")
                                .enableSwagger()
                                .outputDir(projectPath + "src/main/java")
                                .enableSpringdoc()
                                .disableOpenDir()
                                //Java 8 时间格式
                                .dateType(DateType.TIME_PACK)
                                .build()
                )
                //包配置
                .packageConfig(builder ->
                        builder.parent("com.qys.livesMall.shop")
                                .entity("model.entity")
                                //自定义xml生成路径
                                .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper"))
                                .build()
                )
                //策略配置
                .strategyConfig(builder ->
                                builder.addInclude(tables.toArray(strings))
                                        .addTablePrefix("h2_")
                                        .entityBuilder()
                                        .enableLombok()
                                        .logicDeleteColumnName("deleted")
//                                .superClass(BaseEntity.class)
//                                .addSuperEntityColumns("createTime", "creator", "updateTime", "operator")
                                        .idType(IdType.INPUT)
                                        .mapperBuilder()
                                        .controllerBuilder()
                                        .enableRestStyle()
                                        .serviceBuilder()
                                        .formatServiceFileName("%sService")
                                        .enableFileOverride()
                                        .build()
                )
                //自定义模板
//                .templateConfig(builder ->
//                        builder.entity("templates/entity1.java")
//                                .build()
//                )
                //模板引擎
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }
}
